package com.imooc.controller;

import cn.hutool.json.JSON;
import com.imooc.pojo.bo.ShopcartBO;
import imooc.utils.ApiResponse;
import imooc.utils.JsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Api(value = "购物车接口", tags = {"购物车接口相关API"})
@RestController
@RequestMapping("shopcart")
public class ShopcartController {

    final static Logger logger = LoggerFactory.getLogger(ShopcartController.class);

    /**
     * @author cg.hu 
     * @version 2.6      
     * @date 2021/1/28 9:33
     * @description    
     **/
    @ApiOperation(value = "添加商品到购物车", notes = "添加商品到购物车", httpMethod = "POST")
    @PostMapping("/add")
    public ApiResponse add(
            @RequestParam String userId,
            @RequestBody ShopcartBO shopcartBO,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception{
        if (StringUtils.isBlank(userId)) {
            return ApiResponse.ok("");
        }

        logger.info("shopcartBO");
        logger.info(JsonUtils.objectToJson(shopcartBO));

        // TODO 前端用户在登录的情况下，添加商品到购物车，会同时在后端同步购物车到redis缓存
        return ApiResponse.ok();
    }

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/28 13:55
     * @description    删除购物车数据
     **/
    @ApiOperation(value = "删除购物车数据", notes = "删除购物车数据", httpMethod = "POST")
    @PostMapping("/del")
    public ApiResponse del(
            @ApiParam(name = "userId", value = "用户ID", required = true, example = "1")
            @RequestParam String userId,
            @ApiParam(name = "itemSpecId", value = "规格id", required = true, example = "1")
            @RequestParam String itemSpecId) throws Exception {
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(itemSpecId)) {
            return ApiResponse.errorMsg("");
        }

        // TODO 用户在页面删除购物车中的商品数据，
        // 如果此时用户已经登录，则需要同步删除后端购物车中的商品

        //List<ShopcartVO>  shopcartVOList = itemService.queryItemsBySpecIds(itemSpecId);
        return ApiResponse.ok();
    }

}
